package com.xyhuo.leetcode.january;

import java.util.List;

/**
 * 美丽塔Ⅰ
 * https://leetcode.cn/problems/beautiful-towers-i/
 *
 * @time 2024/1/24
 */
public class MaximumSumOfHeights {

    public long maximumSumOfHeights(List<Integer> maxHeights) {
        long ans = 0;
        for (int i = 0; i < maxHeights.size(); i++) {
            int lastHeight = maxHeights.get(i);
            long all = lastHeight;
            for (int j = i - 1; j >= 0; j--) {
                int curHeight = Math.min(maxHeights.get(j), lastHeight);
                lastHeight = curHeight;
                all += curHeight;
            }
            lastHeight = maxHeights.get(i);
            for (int j = i + 1; j < maxHeights.size(); j++) {
                int curHeight = Math.min(lastHeight, maxHeights.get(j));
                lastHeight = curHeight;
                all += curHeight;
            }
            if (all > ans) {
                ans = all;
            }
        }
        return ans;
    }
}
